package com.system.comm.util;

import java.util.ArrayList;
import java.util.List;

import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

/**
 * @author cz
 *
 */
public class AjaxResult {

    /**
     */
    private long code = 0;

    /**
     */
    private Object msg;

    /**
     */
    private long count;

    /**
     */
    private Object data;

    /**
     */
    private String redirect;
    
    private Object data1;
    private Object data2;

    public Object getData1() {
		return data1;
	}

	public void setData1(Object data1) {
		this.data1 = data1;
	}

	public Object getData2() {
		return data2;
	}

	public void setData2(Object data2) {
		this.data2 = data2;
	}

	public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public Object getMsg() {
        return msg;
    }

    public void setMsg(Object msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public AjaxResult result(long code, Object msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }

    public AjaxResult error(Object msg) {
        return result(1, msg);
    }

    public AjaxResult success(Object msg) {
        return result(0, msg);
    }

    public AjaxResult setValidatedResult(BindingResult result) {
        if (result != null && result.hasErrors()) {
            List<String> msgs = new ArrayList<String>();
            List<ObjectError> errors = result.getAllErrors();
            for (ObjectError error : errors) {
                msgs.add(error.getDefaultMessage());
            }
            setCode(400);
            setMsg(msgs);
        }
        return this;
    }

    public String getRedirect() {
        return redirect;
    }

    public void setRedirect(String redirect) {
        this.redirect = redirect;
    }

}
